package com.usdt.common.utils.bean;

import java.lang.reflect.Field;

public class Bean2QueryUtils {

	public static String toQuerystring(Object obj) {

		try {
			StringBuilder queryString = new StringBuilder();
			Class<?> clazz = obj.getClass();
			Field[] fields = clazz.getDeclaredFields();
			int i = 0;
			for (Field field : fields) {
				i ++;
				field.setAccessible(true);
				String fieldName = field.getName();
				Object fieldValue;

				fieldValue = field.get(obj);
				
				if (fieldValue != null) {
					queryString.append(fieldName).append("=").append(fieldValue);
					if (i != fields.length) {
						queryString.append("&");
					}
				}
			}
			return queryString.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
